<?php

function cookie_encrypt($str) {
	return sha1("fsn844dsdskSDFDSs89fs" . $str . "sfn;dlSDF@#$89wdsfkSDSw2");
}

function get_user() {
	global $msg;
	if (isset($_COOKIE['po_page']) && ($_COOKIE['po_page'])) {
		//print_r($_COOKIE);
		
		list($mi, $ming) = explode("|", $_COOKIE['po_page'], 2);
		//$msg = "---" . $mi . "---" . $ming . "---" . cookie_encrypt($ming) . "---";
		//print($ming . "========" . cookie_encrypt($ming) . "||||||" . $mi);
		if (cookie_encrypt($ming) == $mi) {
			//print "++++++";
			$user = unserialize(base64_decode($ming));
			
			if (is_array($user)) {
				return $user;
			}
		} else {
			//print "---------";
		}
	}
	return false;
}

function save_user($user) {
	global $msg;
	$x = & base64_encode(trim(serialize($user)));
	#setcookie("po_page", cookie_encrypt($x) . "|" . $x, time()+3600, "/", ".heihei.org");
	//$msg .= "000 $x 000 " . cookie_encrypt($x) . " 000";
	setcookie("po_page", cookie_encrypt($x) . "|" . $x);
}

function page_common($title) {
	global $t, $user;
	$t->set_file("header", "header.html");
	$t->set_block("header", "mymenu", "hhh");
	$t->set_block("header", "loginlink", "hhh2");
	if (isset($user['username']) && $user['username']) {
		$t->set_var("T_USERNAME", $user['username']);
		$t->parse("hhh", "mymenu");
	} else {
		$t->parse("hhh2", "loginlink");
	}
	$t->set_file("footer", "footer.html");
	$t->set_var("T_PAGETITLE", $title);
	$t->parse("T_HEADER", "header");
	$t->parse("T_FOOTER", "footer");
}

function get_post($name) {
	if (isset($_POST[$name])) {
		return htmlspecialchars(trim($_POST[$name]));
	}
	return "";
}

function get_get($name) {
	if (isset($_GET[$name])) {
		return htmlspecialchars(trim($_GET[$name]));
	}
	return "";
}

function get_myurl() {
	if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
		return $_SERVER['REQUEST_URI'];
	} else if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']){
		return $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];
	} else {
		return $_SERVER['SCRIPT_NAME'];
	}
}

function save_burl() {
	if (preg_match('/log(in|out)\.php$/', $_SERVER['SCRIPT_NAME'])) {
		return;
	}
	global $user;
	$user['burl'] = get_myurl();
	save_user($user);
}

function require_login() {
	global $user;
	if (!isset($user['username'])) {
		header("Location: ".URL_ROOT."login.php");
	}
}

function paging($prefix, $count, $curpageno = 1, $around = 5, $perpage = 20) {
	$r = array();
	$pagecount = ceil($count / $perpage);
	if ($curpageno < 1) {
		$curpageno = 1;
	}
	if ($curpageno > $pagecount) {
		$curpageno = $pagecount;
	}
	$r['pagecount'] = $pagecount;
	$r['start'] = ($curpageno - 1) * $perpage;
	$r['end'] = $curpageno * $perpage;
	$r['perpage'] = $perpage;
	$html = "";
	if ($pagecount > 1) {
		if ($curpageno > 1) {
			$html .= '<a href="' . $prefix . '1">|&lt;&lt;</a> ';
			$lastpageno = $curpageno -1;
			$html .= '<a href="' . $prefix . $lastpageno . '">&lt;</a> ';
		}
		$startno = $curpageno - $around;
		if ($startno < 1) {
			$startno = 1;
		}
		$endno = $startno + $around * 2;
		if ($endno > $pagecount) {
			$endno = $pagecount;
			$startno = $endno - $around * 2;
			if ($startno < 1) {
				$startno = 1;
			}
		}
		if ($startno > 1) {
			$html .= "... ";
		}
		for ($i = $startno;$i<=$endno;$i++) {
			if ($i == $curpageno) {
				$html .= "<b>" . $i . "</b> ";
			} else {
				$html .= '<a href="' . $prefix . $i . '">' . $i . "</a> ";
			}
		}
		if ($endno < $pagecount) {
			$html .= "... ";
		}
		if ($curpageno < $pagecount) {
			$nextpageno = $curpageno +1;
			$html .= '<a href="' . $prefix . $nextpageno . '">&gt;</a> ';
			$html .= '<a href="' . $prefix . $pagecount . '">&gt;&gt;|</a> ';
		}
	}
	$r['curpageno'] = $curpageno;
	$r['html'] = & $html;
	return $r;
}

function is_logon() {
	global $user;
	if (isset($user['username']) && $user['username']) {
		return true;
	}
	return false;
}

function parse_tags($str) {
	$tags = explode(",", $str);
	$tags2 = array();
	$i = 0;
	foreach ($tags as $tag) {
		$tag = trim($tag);
		if (mb_strlen($tag) > 20) {
			$tag = mb_substr($tag, 0, 20);
		}
		$tags2[] = $tag;
		$i++;
		if ($i > 6) {
			break;
		}
	}
	return $tags2;
}

function format_content($str) {
	$array1 = array('/\[img\](\S+)\[\/img\]/i', '/\[code\s+(\w+)\](.+)\[\/code\]/ise', '/\r?\n/');
	$array2 = array('<img src="$1" />', 'format_code(html_defilter(\'\\2\'), "\\1")', '<br />');
	$content = preg_replace($array1, $array2, $str);
	return $content;
}

if (!function_exists("htmlspecialchars_decode")) {
   function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
       return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
   }
}


function format_code($str, $lang="text") {
	$geshi = new GeSHi($str, $lang);
	return "---------- 我是代码的起始线 -----------<br/>" . $geshi->parse_code() . "---------- 我是代码的结束线 -----------<br/>";
}

/*
function html_filter($str) {
	return htmlspecialchars($str);
}*/

function html_defilter($str) {
	return htmlspecialchars_decode($str);
}

function tag_links($tags) {
	$tags2 = parse_tags($tags);
	$tags3 = "";
	foreach ($tags2 as $tag) {
		$tags3 .= '<a href="tag.php?tag=' . $tag . '">' . $tag . '</a> ';
	}
	return trim($tags3);
}